package com.pshcp.service;

import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.framework.util.SystemUtil;
import com.github.pagehelper.PageHelper;
import com.pshcp.common.enums.CommonStatusEnum;
import com.pshcp.domain.dto.DoctorDeptDTO;
import com.pshcp.domain.model.DoctorDept;
import com.pshcp.mapper.DoctorDeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 医生科室信息表 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-08
 */
@Service
public class DoctorDeptService {

    @Autowired
    private DoctorDeptMapper doctorDeptMapper;

    public List<DoctorDept> getDoctorDeptByDoctorId(Long doctorId) {
        Example example = new Example(DoctorDept.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        criteria.andEqualTo("doctorId", doctorId);
        example.setOrderByClause(" created_time desc");
        List<DoctorDept> list = doctorDeptMapper.selectByExample(example);
        return list;
    }

    public DoctorDept getDoctorDeptByHisDeptId(String hisDeptId) {
        Example example = new Example(DoctorDept.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        criteria.andEqualTo("hisDeptId", hisDeptId);
        example.setOrderByClause(" created_time desc");
        List<DoctorDept> list = doctorDeptMapper.selectByExample(example);
        return CollectionUtils.isEmpty(list)?null:list.get(0);
    }

    public List<DoctorDept> getDoctorDeptByDoctorIds(List<Long> doctorIds) {
        Example example = new Example(DoctorDept.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        criteria.andIn("doctorId", doctorIds);
        example.setOrderByClause(" created_time desc");
        List<DoctorDept> list = doctorDeptMapper.selectByExample(example);
        return list;
    }

    public PageList<DoctorDept> list(DoctorDeptDTO doctorDeptDTO) {
        if (doctorDeptDTO.getPageNum() != null && doctorDeptDTO.getPageSize() != null) {
            PageHelper.startPage(doctorDeptDTO.getPageNum(), doctorDeptDTO.getPageSize());
        }
        Example example = new Example(DoctorDept.class);
        Example.Criteria criteria = example.createCriteria();
        if (doctorDeptDTO.getStatus() != null) {
            criteria.andEqualTo("status", doctorDeptDTO.getStatus());
        }
        example.setOrderByClause(" created_time desc");
        List<DoctorDept> list = doctorDeptMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<DoctorDept> select(DoctorDept doctorDept) {
        return doctorDeptMapper.select(doctorDept);
    }

    public int insert(DoctorDept doctorDept) {
        doctorDept.setId(SystemUtil.getId());
        doctorDept.setStatus(CommonStatusEnum.INIT.getCode());
        doctorDept.setCreatedTime(new Date());
        return doctorDeptMapper.insert(doctorDept);
    }

    public int update(DoctorDept doctorDept) {
        doctorDept.setUpdateTime(new Date());
        return doctorDeptMapper.updateByPrimaryKeySelective(doctorDept);
    }

    public DoctorDept findById(Long id) {
        return doctorDeptMapper.selectByPrimaryKey(id);
    }

    public List<DoctorDept> findByIds(List<Long> ids) {
        Example example = new Example(DoctorDept.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return doctorDeptMapper.selectByExample(example);
    }

    public int delById(Long id) {
        Example example = new Example(DoctorDept.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        DoctorDept doctorDept = new DoctorDept();
        doctorDept.setStatus(CommonStatusEnum.DEL.getCode());
        return doctorDeptMapper.updateByExampleSelective(doctorDept, example);
    }

    /**
     * 根据his医生id删除
     *
     * @param hisDoctorIds 医生id数组
     * @return
     */
    public int delDoctorDeptByHisDoctorIds(List<String> hisDoctorIds) {
        Example example = new Example(DoctorDept.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("hisDoctorId", hisDoctorIds);

        return doctorDeptMapper.deleteByExample(example);
    }

}
